package fr.ece.ing4.chatapplication.server;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 *
 * Created by dudu on 27/03/14.
 */
public class ServerFrame extends JFrame implements ActionListener, ChangeListener {

    private ServerController serverController;

    private JPanel leftPanel, rightPanel, globalPanel;

    private JScrollPane chatRoomPane, allUserPane, chatRoomUserPane, scrollConsole;
    private JList<String> chatRoomList, allUserList, chatRoomUserList;
    private DefaultListModel<String> dlm, allDLM, chatroomDLM;

    private JLabel availableChatRoom;

    private JTextArea console;

    private JButton createChatRoom, deleteChatRoom;



    private void initComponents() {

        globalPanel = new JPanel(new GridBagLayout());

        leftPanel = new JPanel(new GridBagLayout());
        rightPanel = new JPanel(new GridBagLayout());

        createChatRoom = new JButton("CREATE CHATROOM");
        deleteChatRoom = new JButton("DELETE CHATROOM");



        availableChatRoom = new JLabel("Chatrooms list");

        allDLM = new DefaultListModel<String>();
        allUserList = new JList<String>(allDLM);
        allUserPane = new JScrollPane(allUserList);

        chatroomDLM = new DefaultListModel<String>();
        chatRoomUserList = new JList<String>(chatroomDLM);
        chatRoomUserPane = new JScrollPane(chatRoomUserList);

        dlm = new DefaultListModel<String>();
        chatRoomList = new JList<String>(dlm);
        chatRoomPane = new JScrollPane(chatRoomList);

        console = new JTextArea("Console:" + "\n");

        scrollConsole = new JScrollPane(console);


    }

    private void setComponentsProperties() {

        // SIZE //

        leftPanel.setMinimumSize(new Dimension(200, 400));
        leftPanel.setPreferredSize(new Dimension(200, 400));
        leftPanel.setSize(new Dimension(200, 400));

        rightPanel.setMinimumSize(new Dimension(400, 400));
        rightPanel.setPreferredSize(new Dimension(400, 400));
        rightPanel.setSize(new Dimension(400, 400));

        scrollConsole.setMinimumSize(new Dimension(400, 200));
        scrollConsole.setPreferredSize(new Dimension(400, 200));
        scrollConsole.setSize(new Dimension(400, 200));

        chatRoomUserPane.setMinimumSize(new Dimension(200, 200));
        chatRoomUserPane.setPreferredSize(new Dimension(200, 200));
        chatRoomUserPane.setSize(new Dimension(200, 200));

        allUserPane.setMinimumSize(new Dimension(200, 200));
        allUserPane.setPreferredSize(new Dimension(200, 200));
        allUserPane.setSize(new Dimension(200, 200));

        createChatRoom.setMinimumSize(new Dimension(200, 55));
        createChatRoom.setPreferredSize(new Dimension(200, 55));
        createChatRoom.setSize(new Dimension(200, 55));

        deleteChatRoom.setMinimumSize(new Dimension(200, 55));
        deleteChatRoom.setPreferredSize(new Dimension(200, 55));
        deleteChatRoom.setSize(new Dimension(200, 55));

        availableChatRoom.setMinimumSize(new Dimension(200, 20));
        availableChatRoom.setPreferredSize(new Dimension(200, 20));
        availableChatRoom.setSize(new Dimension(200, 20));

        chatRoomPane.setMinimumSize(new Dimension(270, 200));
        chatRoomPane.setPreferredSize(new Dimension(270, 200));
        chatRoomPane.setSize(new Dimension(270, 200));

        // BORDERS //

       // chatRoomPane.setBorder(new EtchedBorder(EtchedBorder.RAISED));
    }

    private void addComponentsWithConstraints(){

        GridBagConstraints gbc = new GridBagConstraints();

        gbc.gridx = 0; gbc.gridy = 0; gbc.fill = GridBagConstraints.HORIZONTAL;
        leftPanel.add(availableChatRoom, gbc);
        gbc.gridx = 0; gbc.gridy = 1; gbc.fill = GridBagConstraints.HORIZONTAL;
        leftPanel.add(chatRoomPane, gbc);
        gbc.gridx = 0; gbc.gridy = 2; gbc.fill = GridBagConstraints.HORIZONTAL;
        leftPanel.add(createChatRoom, gbc);
        gbc.gridx = 0; gbc.gridy = 3; gbc.fill = GridBagConstraints.HORIZONTAL;
        leftPanel.add(deleteChatRoom, gbc);

        gbc.gridx = 0; gbc.gridy = 0; gbc.fill = GridBagConstraints.HORIZONTAL;
        rightPanel.add(chatRoomUserPane, gbc);
        gbc.gridx = 1; gbc.gridy = 0; gbc.fill = GridBagConstraints.HORIZONTAL;
        rightPanel.add(allUserPane, gbc);
        gbc.gridx = 0; gbc.gridy = 1; gbc.fill = GridBagConstraints.HORIZONTAL;
        rightPanel.add(scrollConsole, gbc);


        gbc.gridx = 0; gbc.gridy = 0;  gbc.fill = GridBagConstraints.HORIZONTAL;
        globalPanel.add(leftPanel,gbc);
        gbc.gridx = 1; gbc.gridy = 0;  gbc.fill = GridBagConstraints.HORIZONTAL;
        globalPanel.add(rightPanel,gbc);




    }

    private void addComponentsListener() {

    }

    public ServerFrame(ServerController serverController) {

        this.serverController = serverController;

        this.setTitle("Chat Application: " + serverController.getName());

        initComponents();

        setComponentsProperties();

        addComponentsWithConstraints();

        addComponentsListener();

        this.add(globalPanel);

        this.setSize(new Dimension(600, 400));
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setVisible(true);

    }

    @Override
    public void actionPerformed(ActionEvent e) {

    }

    @Override
    public void stateChanged(ChangeEvent e) {


    }

}
